package com.lwj.constant;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author liwenjian
 * @since 2020-05-28
 */
public enum FileMimeEnum {
    /**
     * ceb
     */
    CEB("application/x-ceb", "application/octet-stream"),
    /**
     * word文档
     */
    DOC("application/msword"),
    /**
     * word文档
     */
    DOCX("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
    /**
     * gif动图
     */
    GIF("image/gif"),
    /**
     * jpeg
     */
    JPEG("image/jpeg"),
    /**
     * png
     */
    PNG("image/png"),
    /**
     * pdf
     */
    PDF("application/pdf"),
    /**
     * PPT
     */
    PPT("application/vnd.ms-powerpoint"),
    /**
     * PPT
     */
    PPTX("application/vnd.openxmlformats-officedocument.presentationml.presentation"),
    /**
     * rar
     */
    RAR("application/x-rar-compressed"),
    /**
     * tar
     */
    TAR("application/x-tar"),
    /**
     * txt
     */
    TXT("text/plain", "application/xml"),
    /**
     * wps
     */
    WPS("application/vnd.ms-works"),
    /**
     * xls
     */
    XLS("application/vnd.ms-excel"),
    /**
     * xlsx
     */
    XLSX("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
    /**
     * zip
     */
    ZIP("application/zip"),
    JSP("text/x-jsp"),
    BMP("image/bmp", "image/x-ms-bmp"),
    XML("application/xml"),
    TEXT("text/plain"),
    JS("application/javascript"),
    JPG("image/jpeg"),
    EML("message/rfc822"),
    AVI("video/x-msvideo"),
    DV("video/x-dv"),
    MP4("video/mp4"),
    QUICKTIME("video/quicktime"),
    MPEG("video/mpeg"),
    MPG("video/mpeg"),
    MOV("video/quicktime"),
    WM("video/x-ms-wmv"),
    FLV("video/x-flv"),
    MKV("video/x-matroska"),
    MP3("audio/mpeg"),
    MID("audio/midi"),
    OGG("audio/ogg"),
    MP4A("audio/mp4"),
    WAV("audio/wav"),
    WMA("audio/x-ms-wma"),
    TIF("image/tiff"),
    TIFF("image/tiff"),
    TGA("image/x-targa"),
    PSD("image/vnd.adobe.photoshop"),
    CSV("text/csv"),
    VSD("application/vnd.visio"),
    VSDX("application/vnd.visio2013"),
    //.7z压缩包文件，枚举类不能用数字开头，改成 ：Z7
    Z7("application/x-7z-compressed"),
    JSON("application/json")
    ;

    /**
     * 同一个文件后缀, 可能会被定义多个mime.
     */
    private final String[] mimes;

    FileMimeEnum(String... mimes) {
        this.mimes = mimes;
    }

    public String getSuffix() {
        if ("Z7".equals(this.name())) {
            return "7z";
        }
        return this.name().toLowerCase();
    }

    public String[] getMimes() {
        return this.mimes;
    }

    public static List<FileMimeEnum> of(String mime) {
        List<FileMimeEnum> result = new ArrayList<>();
        for (FileMimeEnum mimeEnum : FileMimeEnum.values()) {
            for (String s : mimeEnum.mimes) {
                if (s.equalsIgnoreCase(mime)) {
                    result.add(mimeEnum);
                    continue;
                }
            }
        }
        if (CollectionUtils.isEmpty(result)) {
            return null;
        } else {
            return result;
        }

    }

    /**
     * 根据文件类型，获取所有可能的后缀名
     *
     * @param types
     * @return
     */
    public static List<String> getFileMimeEnumByType(List<String> types) {
        List<String> result = new ArrayList<>();
        types.stream().forEach(type -> {
            if ("图片".equals(type)) {
                result.addAll(imageType);
            } else if ("视频".equals(type)) {
                result.addAll(videoType);
            }
        });
        return result;
    }
    private static List<String> imageType=new ArrayList<>();
    private static List<String> videoType=new ArrayList<>();
    static {
        imageType=new ArrayList<>(Arrays.asList("gif", "jpeg", "jpeg", "png", "bmp", "jpg", "tif", "tiff", "tga", "psd"));
        videoType=new ArrayList<>(Arrays.asList("avi", "dv", "mp4", "mpeg", "mpg", "mov", "wm", "flv", "mkv"));
    }

    public static Boolean isImage(String fileName){
        return imageType.contains(fileName.split("\\.")[1]);
    }

}

